package com.wenwei.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.orhanobut.logger.Logger;
import com.wenwei.adapter.SearchAddressAdapter;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by nidalee on 2017/9/23.
 */

public class SearchAddressActivity extends BaseActivity implements Inputtips.InputtipsListener,TextWatcher{
    @Bind(R.id.base_title)
    TextView baseTitle;
    @Bind(R.id.search_et)
    EditText searchEt;
    @Bind(R.id.recycler)
    RecyclerView recycler;

    private SearchAddressAdapter mAdapter;

    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_search_address;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("地点搜索");
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new SearchAddressAdapter();
        mAdapter.setmCallBack(new SearchAddressAdapter.CallBack() {
            @Override
            public void onItemClick(Tip t) {
                Intent intent = new Intent();
                intent.putExtra("lng",t.getPoint().getLongitude());
                intent.putExtra("lat",t.getPoint().getLatitude());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        recycler.setAdapter(mAdapter);

        searchEt.addTextChangedListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.base_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void initData() {
        super.initData();


    }

    @Override
    public void onGetInputtips(List<Tip> list, int i) {
        if(i == AMapException.CODE_AMAP_SUCCESS){
            mAdapter.setmList(list);
            mAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        String newText = charSequence.toString().trim();
        InputtipsQuery inputquery = new InputtipsQuery(newText, "成都");
        inputquery.setCityLimit(true);
        Inputtips inputTips = new Inputtips(SearchAddressActivity.this, inputquery);
        inputTips.setInputtipsListener(this);
        inputTips.requestInputtipsAsyn();
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
}
